package com.vshop.platform.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.vshop.framework.core.domain.R;
import com.vshop.framework.core.validation.AddGroup;
import com.vshop.framework.core.validation.EditGroup;
import com.vshop.framework.log.annotation.Log;
import com.vshop.framework.log.enums.BusinessType;
import com.vshop.framework.mybatisflex.page.PageData;
import com.vshop.framework.mybatisflex.page.PageQuery;
import com.vshop.platform.system.crud.service.SysUserService;
import com.vshop.platform.system.domain.param.SysUserParam;
import com.vshop.platform.system.domain.vo.RoleCheckedVo;
import com.vshop.platform.system.domain.vo.SysUserVo;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户信息表 控制层。
 *
 * @author vshop
 * @since 2024-11-14
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/sysUser")
public class SysUserController {

    private final SysUserService sysUserService;

    /**
     * 分页查询用户信息表。
     *
     * @param pageQuery 分页对象
     * @return 分页对象
     */
    @SaCheckPermission("sysUser:list")
    @GetMapping("/page")
    public R<PageData<SysUserVo>> page(@ModelAttribute SysUserParam param,
                                       @ModelAttribute PageQuery pageQuery) {
        return R.ok(sysUserService.page(param, pageQuery));
    }

    /**
     * 添加用户信息表。
     *
     * @param param 用户信息表
     */
    @Log(module = "用户管理", title = "添加用户", businessType = BusinessType.Insert)
    @SaCheckPermission("sysUser:save")
    @PostMapping("/save")
    public R<Void> save(@Validated(AddGroup.class)
                        @RequestBody SysUserParam param) {
        return sysUserService.save(param) ? R.ok() : R.fail();
    }


    /**
     * 根据主键更新用户信息表。
     *
     * @param param 用户信息表
     */
    @Log(module = "用户管理", title = "更新用户", businessType = BusinessType.Update)
    @SaCheckPermission("sysUser:update")
    @PostMapping("/update")
    public R<Void> update(@Validated(EditGroup.class)
                          @RequestBody SysUserParam param) {
        return sysUserService.update(param) ? R.ok() : R.fail();
    }

    /**
     * 根据主键删除用户信息表。
     *
     * @param id 主键
     */
    @Log(module = "用户管理", title = "删除用户", businessType = BusinessType.Delete)
    @SaCheckPermission("sysUser:remove")
    @PostMapping("/remove/{id}")
    public R<Void> remove(@NotNull(message = "主键不能为空")
                          @PathVariable Long id) {
        return sysUserService.removeByIds(List.of(id)) ? R.ok() : R.fail();
    }

    /**
     * 根据主键列表删除用户信息表。
     *
     * @param ids 主键列表
     */
    @Log(module = "用户管理", title = "批量删除用户", businessType = BusinessType.Delete)
    @SaCheckPermission("sysUser:remove")
    @PostMapping("/removeMul/{ids}")
    public R<Void> removeMul(@NotEmpty(message = "主键不能为空")
                             @PathVariable List<Long> ids) {
        return sysUserService.removeByIds(ids) ? R.ok() : R.fail();
    }

    /**
     * 更新用户状态
     */
    @Log(module = "用户管理", title = "更新用户状态", businessType = BusinessType.Update)
    @PostMapping("/changeStatus")
    public R<Void> changeStatus(Long userId, String status) {
        return sysUserService.changeStatus(userId, status) ? R.ok() : R.fail();
    }

    /**
     * 根据用户信息表主键获取详细信息。
     *
     * @param id 用户信息表主键
     * @return 用户信息表详情
     */
    @GetMapping("/getInfo/{id}")
    public R<SysUserVo> getInfo(@NotNull(message = "主键不能为空")
                                @PathVariable Long id) {
        return R.ok(sysUserService.getInfoById(id));
    }


    /**
     * 用户授权角色
     */
    @Log(module = "用户管理", title = "授权角色", businessType = BusinessType.Update)
    @SaCheckPermission("sysUser:auth")
    @PostMapping("/authRole")
    public R<Void> authRole(Long userId, Long[] roleIds) {
        sysUserService.authRole(userId, List.of(roleIds));
        return R.ok();
    }

    /**
     * 根据用户ID获取授权角色 和 查询角色集合
     */
    @SaCheckPermission("sysUser:query")
    @GetMapping("/authRole/{userId}")
    public R<RoleCheckedVo> getAuthRole(@NotNull(message = "主键不能为空")
                                        @PathVariable Long userId) {
        return R.ok(sysUserService.getAuthRole(userId));
    }
}
